package cn.szzsi._web;

import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Receiver;
import cn.szzsi.validator.ReceiverValidator;
import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台类 - 收货地址
 * 
 */
public class ReceiverController extends BaseWebController<Receiver> {
	
	private Receiver receiver;
	private String shopId;//用户的shopId就是商户的id
	// 收货地址列表
	public void list() {
		String shopId=getPara("shopId");
		Member member=getLoginMember();
		String returnUrl=getPara("returnUrl");
		setAttr("receiver", Receiver.dao.getReceiverList(member));
		setAttr("returnurl","returnurl");
		setAttr("from","conform");
		setAttr("useWeixinAddr",true);
		setAttr("returnUrl",returnUrl);
		String productId=getPara("productId");
		String totalQuantity=getPara("totalQuantity");
		if(StrKit.notBlank(productId)){
			setAttr("productId",productId);//totalQuantity
			setAttr("totalQuantity",totalQuantity);
		}
		setAttr("shopId",shopId);
		wapTemplatePath("receiver_list.html");

	}

	// 收货地址添加
	public void add() {
		Member loginMember = getLoginMember();
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为0
		List<Receiver> receiverList = loginMember.getReceiverList();
		if (receiverList != null && Receiver.MAX_RECEIVER_COUNT != null && receiverList.size() >= Receiver.MAX_RECEIVER_COUNT) {
			addActionError("只允许添加最多" + Receiver.MAX_RECEIVER_COUNT + "项收货地址!");
			return;
		}
		getTemplatePath(shopId,"receiver_input");
	}

	// 获取单个地址实例
	public void get() {
		String id=getPara("id");
		Receiver r = Receiver.dao.findById(id);
		renderJson(r);
	}
		
	// 收货地址编辑
	public void edit() {
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为0
		id = getPara("id","");
		if (StrKit.notBlank(id)){
			receiver = Receiver.dao.findById(id);
		}
		if(receiver.equals(getLoginMember().getStr("id"))) {
			addActionError("参数错误!");
			return;
		}
		setAttr("receiver", receiver);
		getTemplatePath(shopId,"receiver_input");
	}

	// 收货地址删除
	public void delete() {
		id = getPara("id","");
		Receiver receiver = Receiver.dao.findById(id);
		if(receiver.equals(getLoginMember().getStr("id"))) {
			renderJson(Ret.fail("info","参数错误!"));
			return;
		}
		receiver.delete();
		renderJson(Ret.ok("info","操作成功!"));
	}

	// 收货地址删除
	public void pcDelete() {
		id = getPara("id","");
		Receiver receiver = Receiver.dao.findById(id);
		if(receiver.equals(getLoginMember().getStr("id"))) {
			addActionError("参数错误!");
			return;
		}
		receiver.delete();
		redirect("/b/receiver/list");
	}

	// 设置默认地址
	public void setdefault() {
		String memberId=getLoginMember().getStr("id");
		id = getPara("id","");
		Db.update("update mall_receiver set isDefault = 0 where member_id=?",memberId);
		Db.update("update mall_receiver set isDefault = 1 where id = ?",id);
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		renderJson(jsonMap);
	}
	
	// 收货地址保存
	@Before(ReceiverValidator.class)
	public void save() {
		receiver = getModel(Receiver.class);
		Member loginMember = getLoginMember();
		List<Receiver> receiverList = loginMember.getReceiverList();
		if (receiverList != null && Receiver.MAX_RECEIVER_COUNT != null && receiverList.size() >= Receiver.MAX_RECEIVER_COUNT) {
			renderJson(Ret.fail("info","只允许添加最多" + Receiver.MAX_RECEIVER_COUNT + "项收货地址!"));
			return;
		}

		receiver.set("member_id", loginMember.getStr("id"));
		if(StrKit.isBlank(receiver.getStr("id"))){
			receiver.save(receiver,loginMember.getStr("id"));
		}else{
			receiver.update(receiver,loginMember.getStr("id"));
		}
		if (receiver.getBoolean("isDefault")) {//如果是默认地址,则将用户手机号更改为该地址
			loginMember.setPhone(receiver.getMobile());
			loginMember.update();

		}
		renderJson(Ret.ok("info","操作成功!"));
	}

	// 收货地址保存
	@Before(ReceiverValidator.class)
	public void pcSave() {
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为0
		receiver = getModel(Receiver.class);
		String areaPath=receiver.getStr("province")+" "+receiver.getStr("city")+" "+receiver.getStr("area");
		if (StrKit.notBlank(receiver.getStr("phone")) && StrKit.notBlank(receiver.getStr("mobile"))) {
			addActionError("联系电话、联系手机必须填写其中一项!");
			return;
		}
		/*if (!Area.dao.isAreaPath(receiver.getStr("areaPath"))) {
			addActionError("地区错误!");
			return;
		}*/
		Member loginMember = getLoginMember();
		List<Receiver> receiverList = loginMember.getReceiverList();
		if (receiverList != null && Receiver.MAX_RECEIVER_COUNT != null && receiverList.size() >= Receiver.MAX_RECEIVER_COUNT) {
			addActionError("只允许添加最多" + Receiver.MAX_RECEIVER_COUNT + "项收货地址!");
			return;
		}

		receiver.set("member_id", loginMember.getStr("id"));
		receiver.set("areaPath",areaPath);
		receiver.save(receiver,getLoginMember().getStr("id"));
		redirect("/b/receiver/list");
	}
	
	// 收货地址更新
	public void update() {
		receiver = getModel(Receiver.class);
		if (StrKit.notBlank(receiver.getStr("phone")) && StrKit.notBlank(receiver.getStr("mobile"))) {
			addActionError("联系电话、联系手机必须填写其中一项!");
			return;
		}
		/*if (!Area.dao.isAreaPath(receiver.getStr("areaPath"))) {
			addActionError("地区错误!");
			return;
		}*/
		Receiver persistent = Receiver.dao.findById(receiver.getStr("id"));
		if(persistent.equals(getLoginMember().getStr("id"))) {
			addActionError("参数错误!");
			return;
		}
		receiver.update(receiver,getLoginMember().getStr("id"));

		if (receiver.getBoolean("isDefault")) {//如果是默认地址,则将用户手机号更改为该地址
			Member loginMember = getLoginMember();
			loginMember.setPhone(receiver.getMobile());
			loginMember.update();

		}
		redirect("/b/receiver/list");
	}
}
